package com.dshore.messagecenter.dao.basedata;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dshore.messagecenter.model.basedata.MdTemplate;

@Repository
public class TemplateDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	/**
	 * 新增模板
	 * @param template
	 * @return
	 */
	public Integer insertTemplate(MdTemplate template) {
		String sql ="insert into T_MD_TEMPLATE(id,template_name,"
				+ "template_content,sample_data,created_time) values (?, ?, ?, ?,?)";
		return jdbcTemplate.update(sql,template.getId(),template.getTemplateName(),
				template.getTemplateContent(),template.getSampleData(),new Date());	
	}
	/**
	 * 修改模板
	 * @param template
	 * @return
	 */
	public Integer updateTemplate(MdTemplate template) { 
		String sql ="update T_MD_TEMPLATE set template_name=?,"
				+ "template_content=?,sample_data=?,modified_time=? where id=?";
		return jdbcTemplate.update(sql,template.getTemplateName(),
				template.getTemplateContent(),template.getSampleData(),new Date(),template.getId()); 
	}
	/**
	 * 删除模板
	 * @param id
	 */
	public void delTemplate(String[] id) {
		
		String sql="update T_MD_TEMPLATE set del_flag=1,modified_time=?  where id =?";	
		 jdbcTemplate.batchUpdate(sql,new BatchPreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
				ps.setString(2, id[i].toString());
			}			
			@Override
			public int getBatchSize() {
				return id.length;
			}
		});
			
		
	}
	/**
	 * 查询单个模板信息
	 * @param id
	 * @return
	 */
	public MdTemplate getById(String id) {
		String sql="select * from T_MD_TEMPLATE where id = ?";
		RowMapper<MdTemplate> mcTemplateRowMapper = new BeanPropertyRowMapper<>(MdTemplate.class);
		return jdbcTemplate.queryForObject(sql, mcTemplateRowMapper,id);
	}
	/**
	 * 模板总记录数
	 * @param template
	 * @return
	 */
	public long queryCountByPrama(MdTemplate template) {
		String templateName = template.getTemplateName();
		List<Object> list=new ArrayList<>();
		StringBuffer sql =new StringBuffer();
		sql.append("select count(1) from T_MD_TEMPLATE  where del_flag = 0 ");
		if(StringUtils.isNotBlank(templateName)) {
			sql.append("and template_name like ?");
			list.add("%"+template.getTemplateName()+"%");
		}
		Object[]args=list.toArray();
		return jdbcTemplate.queryForObject(sql.toString(),args,Long.class);
	}
	/**
	 * 模糊查询数据
	 * @param template
	 * @return
	 */
	public List<MdTemplate> queryListByPrama(MdTemplate template) {
		String templateName = template.getTemplateName();
		List<Object> list=new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from (");
		sql.append("select A.*, rownum rn from (select * from T_MD_TEMPLATE  where del_flag = 0");		
		if(StringUtils.isNotBlank(templateName)) {
			sql.append("and template_name like ?");
			list.add("%"+templateName+"%");
		}
		sql.append(" order by created_time desc ");
		list.add(template.getOffset()+1);
		sql.append(" ) A   )");
		sql.append(" where rn between ? and ? ");				
		list.add(template.getOffset()+template.getLimit());
		Object[]args=list.toArray();
		RowMapper<MdTemplate> templateRowMapper = new BeanPropertyRowMapper<>(MdTemplate.class);
		return jdbcTemplate.query(sql.toString(),args,templateRowMapper);
	}
	
	/**
	 * 查询重复模板名称
	 * @param templatename
	 * @return
	 */
	public List<MdTemplate> checkName(String templatename) {
		String sql="select ID from T_MD_TEMPLATE  where template_name=? and del_flag=0";
		RowMapper<MdTemplate> templateRowMapper = new BeanPropertyRowMapper<>(MdTemplate.class);
		return jdbcTemplate.query(sql, templateRowMapper,templatename);
	}
	
	/**
	 * 删除模板前检查服务接入是否含有模板
	 * @param id
	 * @return
	 */
	public List<Map<String, Object>> selectTemplate(String[] id) {
		StringBuffer sql = new StringBuffer("select * from T_MD_TOPIC where DEL_FLAG=0 and TEMPLATE_ID in (");
		for(int i=0;i<id.length;i++) {
			sql.append(" ? ");
			if(i!=id.length-1) {
				sql.append(" , ");
			}
		}
		sql.append(" ) ");
		Object[] param=id;
		return jdbcTemplate.queryForList(sql.toString(),param);
	}
	
	
}
